﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Schema;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Origin2.Core;
using XNAssist;

namespace Origin2.Framework
{
    /// <summary>
    /// Represents an object in a game, like a player, enemy, bullet, etc.
    /// </summary>
    public class GameObject : IObject, IOriginUpdateable, IOriginDrawable, IVariableContainer, IControllable, ITransformable
    {
        public Transform Transform { get; private set; }

        /// <summary>
        /// A simple constructor
        /// </summary>
        /// <param name="parent"> The IObjectContainer that contains this object </param>
        public GameObject(IObjectContainer parent)
        {
            Parent = parent;
            Transform = new Transform();
            _variables = new Dictionary<string, object>();
            _controls = new BufferedDictionary<string, Control>();
        }

        public virtual void Update(GameTime gameTime)
        {
            foreach (var pair in _controls)
            {
                pair.Value.Update(gameTime);
            }

            _controls.Update();
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            foreach (var pair in _controls)
            {
                pair.Value.Draw(spriteBatch);
            }
        }

        #region IObject Implementation

        public IObjectContainer Parent { get; private set; }

        public bool MarkedForDeletion { get; private set; }

        public void Delete()
        {
            MarkedForDeletion = true;
        }

        #endregion

        #region IVariableContainer Implementation

        private Dictionary<string, object> _variables;

        public void SetVariable(string key, object value)
        {
            if (_variables.ContainsKey(key))
                _variables[key] = value;
            else
                _variables.Add(key, value);
        }

        public object GetVariable(string key)
        {
            return _variables[key];
        }

        public TVariableType GetVariable<TVariableType>(string key)
        {
            return (TVariableType) GetVariable(key);
        }

        public void RemoveVariable(string key)
        {
            _variables.Remove(key);
        }

        public bool ContainsVariable(string key)
        {
            return _variables.ContainsKey(key);
        }

        public bool ContainsVariable(object value)
        {
            return _variables.ContainsValue(value);
        }

        #endregion

        #region IControllable Implementation

        private BufferedDictionary<string, Control> _controls;

        public void AddControl(string name, Control control)
        {
            _controls.Add(name, control);
        }

        public Control GetControl(string name)
        {
            return _controls[name];
        }

        public void RemoveControl(string name)
        {
            _controls.Remove(name);
        }

        public bool ContainsControl(string name)
        {
            return _controls.ContainsKey(name);
        }

        public bool ContainsControl(Control control)
        {
            return _controls.ContainsValue(control);
        }

        public bool ContainsControlType<TControlType>()
        {
            return _controls.Any(x => x.Value.GetType() == typeof (TControlType));
        }

        #endregion
    }
}
